#include <glib.h>

static GMainLoop *s_loop;
static GTimer *s_timer1, *s_timer2;

static gint s_interval1= 1, s_interval2 = 1;
static gint s_count1 = 5, s_count2 = 5;

static gboolean timeout1_cbk (gpointer user_data)
{
    if(0 == s_count1) {
        g_print("[%f] mainloop(timeout1_cbk) cbk quit! \n", g_timer_elapsed(s_timer1, NULL));
        if(0 == s_count2) {
            g_main_loop_quit(s_loop);
        }

        return FALSE;
    } else {
        g_print("[%f] mainloop(timeout1_cbk) count=%d \n", g_timer_elapsed(s_timer1, NULL), s_count1);
        s_count1--;
    }

    return TRUE;
}

static gboolean timeout2_cbk (gpointer user_data)
{
    if(0 == s_count2) {
        g_print("[%f] mainloop(timeout2_cbk) cbk quit! \n", g_timer_elapsed(s_timer2, NULL));
        if(0 == s_count1) {
            g_main_loop_quit(s_loop);
        }

        return FALSE;
    } else {
        g_print("[%f] mainloop(timeout2_cbk) count=%d \n", g_timer_elapsed(s_timer2, NULL), s_count2);
        s_count2--;
    }

    return TRUE;
}

gint main (gint argc, gchar **argv)
{
    GMainContext *context;
    GSource *tsource1, *tsource2;
    GSource *isource;

    context = g_main_context_new();

    tsource1 = g_timeout_source_new(s_interval1);
    g_source_set_priority (tsource1, G_PRIORITY_LOW);
    g_source_set_callback(tsource1, timeout1_cbk, NULL, NULL);
    g_source_attach(tsource1, context);

    tsource2 = g_timeout_source_new(s_interval2);
    g_source_set_priority (tsource2, G_PRIORITY_HIGH);
    g_source_set_callback(tsource2, timeout2_cbk, NULL, NULL);
    g_source_attach(tsource2, context);


    s_timer1 = g_timer_new();
    g_timer_start(s_timer1);
    
    s_timer2 = g_timer_new();
    g_timer_start(s_timer2);

    s_loop = g_main_loop_new(context, FALSE);
    g_main_loop_run(s_loop);

    g_main_loop_unref(s_loop);
    g_main_context_unref(context);

    g_source_unref(tsource1);
    g_source_unref(tsource2);

    g_timer_stop(s_timer1);
    g_timer_destroy(s_timer1);
    g_timer_stop(s_timer2);
    g_timer_destroy(s_timer2);

    return 0;
}
